from rest_framework import serializers
from drf_haystack.serializers import HaystackSerializer

from .models import SKU
from .search_indexes import SKUIndex


class SKUListSerializer(serializers.ModelSerializer):
    """商品列表的序列化器"""
    class Meta:
        model = SKU
        fields =['id', 'name', 'price', 'default_image_url', 'comments']


class SKUIndexSerializer(HaystackSerializer):
    """索引结果数据序列化器"""
    object = SKUListSerializer(read_only=True)

    class Meta:
        index_classes = [SKUIndex]
        fields = ['text', 'object']